home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / thesource6.dms / thesource6.adf / Source / Misc / AmiProcess.lha / AmiProcess.h
Encoding:
C/C++ Source or Header  |  1993-06-12  |  4.0 KB  |  193 lines

  1. Path: usenet.ee.pdx.edu!fastrac.llnl.gov!lll-winken.llnl.gov!uwm.edu!ux1.cso.uiuc.edu!howland.reston.ans.net!xlink.net!math.fu-berlin.de!news.th-darmstadt.de!hotb.RoBIN.de!batman.RoBIN.de!mania.RoBIN.de!mania!lkv
  2. From: lkv@mania.RoBIN.de (Lutz Vieweg)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Request for C++ Task Class for Multitasking on Amiga
  5. Message-ID: <VS72t*X43@mania.RoBIN.de>
  6. Date: Tue, 08 Jun 1993 15:22:17 GMT
  7. References: <2C0E1CDB.5AE9@deneva.sdd.trw.com>
  8. Organization: The Funny Farm
  9. X-Newsreader: Arn V1.00 alpha rel3
  10. Lines: 180
  11.  
  12. In article <2C0E1CDB.5AE9@deneva.sdd.trw.com>, Scott Karlin writes:
  13.  
  14. > I'm looking for a C++ task class for the Amiga.  Has anyone
  15. > implemented such a thing?  (I'm using Comeau C++ with SAS/C.)
  16.  
  17.  
  18. I've implemented a Process class for GNU C++, maybe it can give
  19. you some ideas.
  20.  
  21. Needs DOSLib (a class wich simply provides opening and closing of the
  22. dos.library) and some simple external error-handling routines.
  23.  
  24. ------------- AmiProcess.h ---------------
  25.  
  26. #ifndef AmiProcess_h
  27. #define AmiProcess_h
  28.  
  29. extern "C" {
  30.     #include <exec/types.h>
  31.     #include <exec/tasks.h>
  32.     #include <inline/exec.h>
  33.     
  34. }
  35.  
  36. #include "DOSLib.h"
  37.  
  38. class AmiProcess {
  39.     
  40.     DOSLib dl;
  41.     
  42.     volatile char run_flag;
  43.     volatile char start_flag;
  44.     long atn_signal;
  45.     long atn_signal_num;
  46.     struct Task * pro_task;
  47.     void (*func)(void);
  48.     static unsigned long thisbuf;
  49.     
  50.     void ProcUpStart(void);
  51.     
  52. public:
  53.     char fail;
  54.     
  55.     AmiProcess(void (*funktion)(), unsigned long int StackSize = 4096,
  56.                char * ProcName = "New Process");
  57.                
  58.          
  59.     ~AmiProcess();
  60.     void leave();
  61.     
  62.     void start(void);
  63.     
  64.     void signal(long sigs);
  65.     
  66.     inline volatile char running(void) {return run_flag;};
  67.     inline long get_atn_bit(void) {return atn_signal;};
  68.     inline int test_atn(void) {return (SetSignal(0L,0L) & atn_signal);};
  69. };
  70.  
  71. #endif AmiProcess_h
  72.  
  73. ------------------------- AmiProcess.cc ------------------------
  74.  
  75. #include "AmiProcess.h"
  76.  
  77. #include "set_error.h"
  78.  
  79. extern "C" {
  80.     #include <dos/dostags.h>
  81. }
  82.  
  83. unsigned long AmiProcess::thisbuf = 0;
  84.  
  85. extern "C" void AmiProcessRealUpstart(void);
  86.  
  87. void AmiProcess::ProcUpStart(void) {
  88.         
  89.         asm("_AmiProcessRealUpstart:
  90.              movel %1,%0" : "=r" (this) : "m" (thisbuf));
  91.         
  92.         if ((atn_signal_num = AllocSignal(-1L))==-1) {
  93.             set_error("AmiProcess::ProcUpStart ","cannot allocate atn_signal");
  94.         }
  95.         
  96.         atn_signal = 1 << atn_signal_num;
  97.         
  98.         pro_task = FindTask(0L);
  99.         
  100.         run_flag = 1;
  101.         
  102.         Wait(atn_signal);
  103.         
  104.         if (start_flag) (*func)();
  105.         
  106.         FreeSignal(atn_signal_num);
  107.         
  108.         run_flag=0;
  109.         
  110.         asm("rts");
  111. }
  112.  
  113. AmiProcess::AmiProcess(void (*funktion)(), unsigned long int StackSize,
  114.                         char * ProcName) : dl(), fail(0) {
  115.     
  116.     asm("movel %1,%0" : "=m" (thisbuf) : "r" (this));
  117.     
  118.     run_flag = start_flag = 0;
  119.     
  120.     func = funktion;
  121.     
  122.     if (!(CreateNewProcTags(NP_Entry, (ULONG)&AmiProcessRealUpstart,
  123.                           NP_Input, Input(),
  124.                           NP_Output, Output(),
  125.                           NP_StackSize, StackSize,
  126.                           NP_Name, (ULONG)ProcName,
  127.                           NP_CloseInput, (ULONG) 0,
  128.                           NP_CloseOutput, (ULONG) 0,
  129.                           TAG_END, 0L))) {
  130.         set_error("AmiProcess::AmiProcess ","CreateNewProc() failed");
  131.         fail = -1;
  132.     }
  133.     else {
  134.         for (int i=0; i<30; i++) {
  135.             if (run_flag) break;
  136.             Delay(5);
  137.         }
  138.         if (!(run_flag)) {
  139.             set_error("AmiProcess::AmiProcess ","child does not start up");
  140.             fail = -1;
  141.         }
  142.     }
  143.     
  144.     
  145. }
  146.  
  147.  
  148. AmiProcess::~AmiProcess () {
  149.     
  150.     leave();
  151.     
  152. }
  153.  
  154. void AmiProcess::leave(void) {
  155.     
  156.     int i;
  157.     
  158.     if (run_flag) {
  159.         
  160.         start_flag = 0;
  161.         Signal(pro_task,atn_signal);
  162.         
  163.         for (i=0; i<30; i++) {
  164.             if (!(run_flag)) break;
  165.             Delay(5);
  166.         }
  167.         
  168.         if (run_flag) set_error("AmiProcess::leave ","child don't wanna exit");
  169.     }
  170. }
  171.  
  172. void AmiProcess::start() {
  173.     
  174.     start_flag = 1;
  175.     Signal(pro_task,atn_signal);
  176.     
  177. }
  178.  
  179. void AmiProcess::signal(long sigs) {
  180.     
  181.     if (run_flag) {
  182.         Signal(pro_task,sigs);
  183.     }
  184. }
  185.  
  186. -----------------------------------------------------------------
  187.  
  188. Flames about code-style, documentation and so on >NIL:
  189.  
  190. cu, Lutz Vieweg
  191.  
  192.  
  193.